// pages/joinProject/joinProject.js
import {
  queryMyResumeAllService
} from "../../api/resume";

Page({
  data: {
    resumeList: [],
    loading: true,
  },

  addProject(){
    wx.navigateTo({
      url: '/pages/addProject/addProject',
    })
  },

  onLoad() {
    this.getMyResumeProjects();
  },

  getMyResumeProjects() {
    const token = wx.getStorageSync("token");
    if (!token) {
      wx.showModal({
        title: "提示",
        content: "请先登录",
        showCancel: true,
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: "/pages/login/login",
            });
          }
        },
      });
      return;
    }

    this.setData({
      loading: true,
    });

    queryMyResumeAllService()
      .then((res) => {
        console.log("获取简历项目列表响应:", res);
        if (res.code === 200) {
          let processedResumes = [];
          // 检查是否有项目数据
          if (res.data && Array.isArray(res.data) && res.data.length > 0) {
            processedResumes = res.data.map((resume) => {
              const joinTime = resume.joinTime ? resume.joinTime.split(' ')[0].replace(/-/g, '.') : '';
              let endTime = '';
              if (resume.endTime === "至今") {
                endTime = "至今";
              } else if (resume.endTime) {
                endTime = resume.endTime.split(' ')[0].replace(/-/g, '.');
              }
              
              let projectBelief = resume.projectBelief;
              if (projectBelief === "1") {
                projectBelief = "落地";
              } else if (projectBelief === "2") {
                projectBelief = "获奖";
              } else if (projectBelief === "3") {
                projectBelief = "学习";
              }

              return {
                ...resume,
                joinTime,
                endTime,
                projectBelief
              };
            });
          }
          // 无论是否有数据，都更新 resumeList
          this.setData({
            resumeList: processedResumes,
            loading: false,
          });
        } else {
          console.log("API响应状态码不是200：", res.code);
          // 检查是否是"暂时还没有项目简历哈"的情况
          if (res.message && res.message.includes("暂时还没有项目")) {
            // 没有项目的情况，清空列表但不显示错误
            this.setData({
              resumeList: [],
              loading: false,
            });
            wx.showToast({
              title: "暂时还没有项目简历",
              icon: "none",
            });
          } else {
            // 其他错误情况
            wx.showToast({
              title: `请求失败: ${res.message || res.code}`,
              icon: "none",
            });
            this.setData({
              resumeList: [],
              loading: false,
            });
          }
        }
      })
      .catch((err) => {
        console.error("获取简历项目列表失败：", err);
        // 只有在网络异常时才显示"网络异常，请重试"
        wx.showToast({
          title: "暂时还没有项目简历",
          icon: "none",
        });
        // 网络异常时也要清空列表
        this.setData({
          resumeList: [],
          loading: false,
        });
      });
  },

  // 刷新数据的方法
  refreshData() {
    console.log("刷新简历项目列表数据");
    this.getMyResumeProjects();
  },

  detailProject(e) {
    const resumeId = e.currentTarget.dataset.resumeId;
    console.log('跳转到简历项目详情，项目ID:', resumeId);

    if (!resumeId) {
      wx.showToast({
        title: "项目信息错误",
        icon: "error",
        duration: 2000,
      });
      return;
    }

    wx.navigateTo({
      url: `/pages/detailProject/detailProject?id=${resumeId}`,
    });
  },

  onReady() {

  },

  onShow() {
    // 每次显示页面时都刷新数据，确保删除项目后能及时更新
    setTimeout(() => {
      this.getMyResumeProjects();
    }, 100);
  },
})